const db = wx.cloud.database();

require("../../../utils/sensorsdata.min");

var t = getApp();

Page({
    data: {
        edit: !1,
        total: 0,
        myList: [],
        delete_ids: [],
        choose_num: 0,
        contrast: [],
        city_list: [],
        code_list: [],
        title_list: [],
        closeHint: !0,
        subscribe: !0
    },
    onShow: function () {
        this.setData({
            contrast: []
        }), this.getCollectList();
    },
    onLoad: function (t) {

        wx.getStorageSync("subscribe") && this.setData({
            subscribe: !0
        });
    },
    getCollectList: function () {
        var e = this;
        db.collection('fav').get({
            success: function (res) {
                console.log(res.data)
                e.setData({
                    myList: res.data,
                    success: !0
                })
            }
        });
    },
    delfav: function (e) {
        var i = e.currentTarget.dataset.id;
        console.log(e.currentTarget)
        var that = this;
        db.collection('fav').where({
            _id: i
        }).remove().then(res => {
            console.log(res)
        });
        this.getCollectList()
    },
    choose: function (t) {
         console.log(t.currentTarget.dataset.zdy);

        for (var e = t.currentTarget.dataset.code, s = t.currentTarget.dataset.zdy, i = t.currentTarget.dataset.code, a = t.currentTarget.dataset.title, o = this.data.myList, n = t.currentTarget.dataset.index, c = this.data.choose_num, l = this.data.contrast, r = this.data.city_list, d = this.data.code_list, u = this.data.title_list, h = 0; h < o.length; h++) o[h].choose && c++;
    //    console.log(s)
    //    console.log(r)
    //    console.log(r.length)
       if(r.length == 1 && s != r){
           var that = this;
           var zdys = [];
           zdys.push(s);
           zdys.push(r[0]);
           console.log(zdys)
           that.setData({
            zdys
           })
           
       } else {
        console.log('+++++++++++++++++++++++++++++')
       }
        if (c > 1 && !o[n].choose) return wx.showToast({
            title: "一次只能对比两个职位",
            icon: "none"
        }), !1;
        o[n].choose = !o[n].choose, o[n].choose ? (l.push(e), r.push(s), d.push(i), u.push(a)) : (l.splice(l.indexOf(e), 1),
                r.splice(r.indexOf(s), 1), d.splice(d.indexOf(i), 1), u.splice(u.indexOf(a), 1)),
            this.setData({
                myList: o
            });
        // console.log(this.data.myList)
    },
    edit: function () {
        var t = this.data.myList,
            e = this.data.edit;
        e = !e;
        for (var s in t) t[s].txtStyle = "";
        this.setData({
            myList: t,
            edit: e
        });
    },
    returnChoose: function () {
        for (var t = this.data.myList, e = 0; e < t.length; e++) t[e].selected = !1;
        this.setData({
            myList: t,
            selectedAll: !1,
            total: 0
        }), this.setData({
            edit: !1
        });
    },
    // bindCheckbox: function (t) {
    //     var e = this,
    //         s = t.currentTarget.dataset.index,
    //         i = t.currentTarget.dataset.rid,
    //         a = [],
    //         o = e.data.myList,
    //         n = t.currentTarget.dataset.id;
    //     if (this.data.edit) {
    //         this.data.total;
    //         o[s].selected = !o[s].selected, o[s].rid = i;
    //         for (var c = 0; c < o.length; c++) o[c].selected && a.push(o[c].rid);
    //         a.length == o.length ? this.setData({
    //             selectedAll: !0
    //         }) : this.setData({
    //             selectedAll: !1
    //         }), e.setData({
    //             myList: o,
    //             total: a.length,
    //             delete_ids: a
    //         });
    //     } else {
    //         o[s].info.zwk_click++, e.setData({
    //             myList: o
    //         });
    //         var l = [];
    //         l.push(o[s].info.zwk_zy, o[s].info.zwk_yl_one, o[s].info.zwk_yl_two), wx.navigateTo({
    //             url: "../jobInfo/jobInfo?id=" + n + "&city=" + o[s].info.zwk_diqu + "&major=" + l + "&education=" + o[s].info.zwk_xl + "&working_life=" + o[s].info.zwk_jcnx + "&person_count=" + o[s].info.zwk_zkrs + "&year=" + o[s].info.zwk_year + "&click_number=" + parseInt(s + 1)
    //         });
    //     }
    // },
    checkedAll: function () {
        var t = [],
            e = this.data.selectedAll,
            s = this.data.myList,
            i = 0;
        s.length > 0 && (e = !e);
        for (var a = 0; a < s.length; a++) s[a].selected = e, s[a].selected && (t.push(s[a].id),
            i++);
        this.setData({
            myList: s,
            selectedAll: e,
            total: i,
            delete_ids: t
        });
    },
    deleteItem: function () {
        if (0 != this.data.total) {
            var e = this.data.myList,
                s = [],
                i = this,
                a = this.data.contrast;
            for (var o in e) 1 != e[o].selected && s.push(e[o]);
            a.pop(this.data.delete_ids), wx.request({
                url: t.globalData.request_url + "api/zhiweiku/gk/getCollect.php",
                method: "POST",
                data: {
                    act: "del_collect",
                    openid: wx.getStorageSync("openId"),
                    id: i.data.delete_ids
                },
                header: {
                    "content-type": "application/x-www-form-urlencoded"
                },
                success: function (t) {
                    200 == t.data.code ? (i.subcribeMsg("deleteQueue", i.data.delete_ids, "none"), i.setData({
                        myList: s,
                        selectedAll: !1,
                        total: 0
                    }), wx.showToast({
                        icon: "none",
                        title: t.data.msg
                    })) : wx.showToast({
                        icon: "none",
                        title: t.data.msg
                    });
                }
            });
        } else wx.showToast({
            icon: "none",
            title: "请选择删除项目"
        });
    },
    onShareAppMessage: function (t) {
        return {
            title: "国考小助手",
            path: "/pages/index/index"
        };
    },
    goContrast: function () {
        var t = this.data.zdys;
        console.log(t)
        2 == t.length ? wx.navigateTo({
            url: "../contrast/contrast?zdy0=" + t[0] + "&zdy1=" + t[1]
        }) : 1 == t.length || 0 == t.length ? wx.showToast({
            title: "请先选择两个职位",
            icon: "none"
        }) : wx.showToast({
            title: "一次只能对比两个职位",
            icon: "none"
        });
    },
    closeHint: function () {
        this.setData({
            closeHint: !0
        });
    },
    subcribeMsg: function (e, s, i) {
        var a = this,
            o = this.data.myList;
        wx.request({
            url: t.globalData.request_url + "api/zhiweiku/gk/zw_push.php",
            method: "POST",
            data: {
                action: e,
                zw_id: s,
                openid: wx.getStorageSync("openId")
            },
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            success: function (t) {
                200 == t.data.code ? "none" != i && ("focus_all" == i ? wx.showToast({
                    title: t.data.msg,
                    icon: "none"
                }) : (wx.showToast({
                    title: t.data.msg,
                    icon: "none"
                }), o[i].is_subscribe = "addQueue" == e ? 1 : 0, a.setData({
                    myList: o,
                    closeHint: !0
                }))) : wx.showToast({
                    title: t.data.msg,
                    icon: "none",
                    duration: 2e3
                });
            }
        });
    },
    getSubcribeMsgs: function () {
        0 != this.data.total ? (this.subcribeMsg("addQueue", this.data.delete_ids, "focus_all"),
            this.setData({
                edit: !1,
                selectedAll: !1,
                total: 0
            }), this.getCollectList()) : wx.showToast({
            icon: "none",
            title: "请选择要关注的职位"
        });
    },
    getSubcribeMsg: function (t) {
        var e = t.currentTarget.dataset.id,
            s = t.currentTarget.dataset.type,
            i = t.currentTarget.dataset.index,
            a = this;
        if (this.setData({
                alertIndex: i,
                alertId: e
            }), "deleteQueue" == s) return a.subcribeMsg(s, e, i), !1;
        var o = "B2Nk3iP5-mIAtq2irhGRTZoi1j1XBfc02G9KmFpMjHU";
        "addQueue" == s && wx.getSetting({
            withSubscriptions: !0,
            success: function (t) {
                console.log(t);
                var n = t.subscriptionsSetting;
                void 0 != n && (0 == n.mainSwitch || void 0 != n.itemSettings && "reject" == n.itemSettings[o]) ? wx.showModal({
                    title: "提示",
                    content: "订阅消息为拒收，请前往设置开启",
                    showCancel: !1,
                    success: function (t) {
                        t.confirm ? (wx.openSetting({
                            withSubscriptions: !0,
                            success: function (t) {
                                console.log("open"), console.log(t);
                            }
                        }), a.setData({
                            closeHint: !1
                        })) : t.cancel && console.log("用户点击取消");
                    }
                }) : a.requestSubscribeApi(o, s, e, i);
            }
        });
    },
    requestSubscribeApi: function (t, e, s, i) {
        var a = this;
        wx.requestSubscribeMessage({
            tmplIds: [t],
            success: function (o) {
                console.log("success"), console.log(o), "requestSubscribeMessage:ok" == o.errMsg && ("accept" == o[t] && (wx.setStorageSync("subscribe", 1),
                    a.subcribeMsg(e, s, i), a.setData({
                        subscribe: !0
                    })), "reject" == o[t] && a.setData({
                    closeHint: !1
                }));
            },
            fail: function (t) {
                console.log("fail"), console.log(t), "requestSubscribeMessage:fail cancel" == t.errMsg && a.setData({
                    closeHint: !1
                });
            }
        });
    },
    gozx: function () {
        wx.navigateTo({
          url: '../../xiaoneng/xiaoneng',
        })
    },
     /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        var area = wx.getStorageSync('area')
        var agent = wx.getStorageSync('agent')
        var page = wx.getStorageSync('page')
        return {
            title: '2021国家公务员招录考试职位表',
            path: 'pages/search/search/search?area=' + area + '&agent=' + agent + '&page=' + page
        }
    },
    /**分享到朋友圈*/
    onShareTimeline: function () {
        var area = wx.getStorageSync('area')
        var agent = wx.getStorageSync('agent')
        var page = wx.getStorageSync('page')
        return {
            title: '2021国家公务员招录考试职位表',
            path: 'pages/search/search/search?area=' + area + '&agent=' + agent + '&page=' + page
        }
    }
});